package com.hly.musicbackground.model.music.rest;

import com.hly.musicbackground.Utils.ThreadPoolUtils;
import com.hly.musicbackground.filter.AuthRoot;
import com.hly.musicbackground.model.music.VO.LyrVO;
import com.hly.musicbackground.model.music.VO.LyricMp3VO;
import com.hly.musicbackground.model.music.params.MusicRidParams;
import com.hly.musicbackground.model.music.service.LyricService;
import com.hly.musicbackground.model.music.service.SongDateilService;
import com.hly.musicbackground.model.music.service.SongService;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 歌曲
 * */
@RestController
@RequestMapping("/api/music")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Log4j2
@Api(tags = "歌曲")
public class MusicController {

    private final SongDateilService songDateilService;
    private final SongService songService;
    private final LyricService lyricService;

    /**
     * 获取歌曲
     * @param musicRidParams 歌曲的rid
     * */
    @GetMapping("/mp3")
    @ApiOperation(value = "获取歌曲链接")
    public Result<String> musicMp3(MusicRidParams musicRidParams){
        String lrc = songDateilService.getMp3(musicRidParams.getRid());
        return Result.ok(lrc);
    }

    /**
     * 获取mv
     * @param musicRidParams 歌曲的rid
     * */
    @GetMapping("/mp4")
    @ApiOperation(value = "获取mv链接")
    public Result<String> musicMp4(MusicRidParams musicRidParams){
        String lrc = songDateilService.getMp4(musicRidParams.getRid());
        return Result.ok(lrc);
    }

    /**
     * 获取歌词
     * @param musicRidParams 歌曲的rid
     * */
    @GetMapping("/lyric")
    @ApiOperation(value = "获取歌词")
    public Result<List<LyrVO>> lyric(MusicRidParams musicRidParams){
        List<LyrVO> lrc = lyricService.getLyric(musicRidParams.getRid());
        return Result.ok(lrc);
    }

    /**
     * 获取歌词歌曲
     * @param musicRidParams 歌曲的rid
     * */
    @GetMapping("/lyricMp3")
    @ApiOperation(value = "获取歌词歌曲")
    public Result<LyricMp3VO> lyricMp3(MusicRidParams musicRidParams){
        return Result.ok(new LyricMp3VO(lyricService.getLyric(musicRidParams.getRid()),
                songDateilService.getMp3(musicRidParams.getRid())));
    }

    /**
     * 歌曲播放时发生异常，下载歌曲
     * @param musicRidParams
     * */
    @GetMapping("/error")
    @ApiOperation(value = "歌曲播放时发生异常，下载歌曲")
    public Result<String> error(MusicRidParams musicRidParams,
                                @RequestParam(defaultValue = "mp3") String type){
        String s = songDateilService.downServer(musicRidParams.getRid(), type);
        return Result.ok(s);
    }

    /**
     * MV播放时发生异常，记录
     * @param musicRidParams
     * */
    @GetMapping("/errorMV")
    @ApiOperation(value = "MV播放时发生异常，记录")
    public Result errorMV(MusicRidParams musicRidParams){
        songService.error(musicRidParams.getRid());
        return Result.ok();
    }

    /**
     * 载歌曲或者音乐
     * @param musicRidParams
     * */
    @GetMapping("/downServer")
    @ApiOperation(value = "载歌曲或者音乐")
    public Result downServer(MusicRidParams musicRidParams,
                             @RequestParam(defaultValue = "mp3") String type){
        songDateilService.downServer(musicRidParams.getRid(), type);
        return Result.ok();
    }

    /**
     * 爬取歌曲
     * @param musicRidParams
     * */
    @GetMapping("/regularTime")
    @ApiOperation(value = "禁止调用")
    public Result regularTime(MusicRidParams musicRidParams){
        songDateilService.regularTime(musicRidParams);
        return Result.ok();
    }
}
